/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.stip.server.da;

import com.stip.server.entities.Activeties;
import com.stip.server.entities.Organization;
import java.awt.print.Book;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author BlueBerry
 */
@Stateless
@LocalBean
public class OrgDA {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @PersistenceContext
    EntityManager entityManager;
       
    public Organization orgFindByID(Integer id){
    Organization organization=entityManager.find(Organization.class, id);
    return  organization;
    }
    public Integer createOrg(String org_name,String email,String qq,String phone,String abstracts){
    Organization organization=new Organization();
    organization.setAbstracts(abstracts);
    organization.setEmail(email);
    organization.setOrgName(org_name);
    
    organization.setPhone(phone);
    organization.setQq(qq);
    entityManager.persist(organization);
    entityManager.flush();
    Integer id=organization.getId();
    
    Query query=entityManager.createQuery("SELECT a FROM Activeties a WHERE a.firstOrg = :firstOrg");
    query.setParameter("firstOrg", organization);
    List<Activeties> activetieses=new ArrayList<Activeties>();
    List list1=query.getResultList();
        for (Iterator it = list1.iterator(); it.hasNext();) {
            Activeties activety = (Activeties)it.next();
            activetieses.add(activety);
        }
    organization.setActivetiesList(activetieses);
   Query query1=entityManager.createQuery("SELECT a FROM Activeties a WHERE a.secondOrg = :secondOrg");
   List<Activeties>activetieses1=new ArrayList<Activeties>();
   List list2=query1.getResultList();
        for (Iterator it = list2.iterator(); it.hasNext();) {
            Activeties activety = (Activeties)it.next();
            activetieses1.add(activety);
           
        }
        organization.setActivetiesList1(activetieses1);
   Query query2=entityManager.createQuery("SELECT a FROM Activeties a WHERE a.thirdOrg = :thirdOrg");
   List<Activeties>activetieses2=new ArrayList<Activeties>();
   List list3=query2.getResultList();
        for (Iterator it = list3.iterator(); it.hasNext();) {
            Activeties activety = (Activeties)it.next();
            activetieses2.add(activety);
        }
   organization.setActivetiesList2(activetieses2);
    return  id;
    }
    public Boolean editOrg(Integer id,String org_name,String email,String qq,String phone,String abstracts){
    Organization organization=entityManager.find(Organization.class, id);
        if (organization!=null) {
            if (abstracts==null) {
                organization.setAbstracts(organization.getAbstracts());
            } else {
                organization.setAbstracts(abstracts);
            }
            if (phone==null) {
                organization.setPhone(organization.getPhone());
            } else {
                organization.setPhone(phone);
            }
            if (qq==null) {
                organization.setPhone(organization.getPhone());
            } else {
                organization.setPhone(phone);
            }
            if (email==null)
            {
                organization.setEmail(organization.getPhone());
            } else {
            organization.setEmail(email);
            
            }
           
           entityManager.merge(organization);
           return  true;
          
        }
        else{return  false;}
    }
    public Boolean deleteOrg(Integer id){
    Organization organization=entityManager.find(Organization.class, id);
        if (organization==null) {
            return false;
        } else {
            entityManager.remove(organization);
            return  true;
        }
    }
}
